package top.joylee.example.grpc;

import top.joylee.example.grpc.route.Feature;
import top.joylee.example.grpc.route.FeatureDataBase;
import top.joylee.example.grpc.route.Point;
import com.google.protobuf.util.JsonFormat;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

public final class RouteGuideUtil {
    private static final double COORD_FACTOR = 1e7;

    public static double getLatitude(Point location) {
        return location.getLatitude() / COORD_FACTOR;
    }

    public static double getLongitude(Point location) {
        return location.getLongitude() / COORD_FACTOR;
    }

    public static URL getDefaultFeaturesFile() {
        return RouteGuideServer.class.getResource("route_guide_db.json");
    }

    public static List<Feature> parseFeatures(URL file) throws IOException {
        try (InputStream input = file.openStream()) {
            try (Reader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) {
                FeatureDataBase.Builder database = FeatureDataBase.newBuilder();
                JsonFormat.parser().merge(reader, database);
                return database.getFeatureList();
            }
        }
    }

    public static boolean exists(Feature feature) {
        return Objects.nonNull(feature) && !feature.getName().isEmpty();
    }
}
